/**
 * 客户端使用Ajax技术获取服务端数据
 * 多个para参数必须使用`间隔
 * @author dyh 2011-02-05
 */
package com.zjht.gmcc.servlet.provider;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zjht.gmcc.bean.provide.Parameter;
import com.zjht.gmcc.bean.util.InitParam;
import com.zjht.gmcc.bean.util.MyFormat;
import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MyParam;

public class AjaxProvider extends HttpServlet {

	private static final long serialVersionUID = 1135302774945453643L;

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doAll(request, response);
	}
	public void toJson(Object obj,HttpServletResponse response) throws Exception{
		Gson gson = new Gson();

		String result = gson.toJson(obj);

	
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");

		PrintWriter out = response.getWriter();
		out.write(result);

		out.flush();
		out.close();
	}
	private void doAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=GBK");
		response.setHeader("Cache-Control", "no-cache");
		request.setCharacterEncoding("UTF-8");
		int i_flag = 0;
		// ---参数
		String key = request.getParameter("key");
		String pareID= request.getParameter("pareID");
		if (MyFormat.isStrNull(key))
			return;
		key=key.trim().toUpperCase();
		String s_sql = null;
		PreparedStatement ps = null;
		ResultSet rs,rs1 = null;
		
		
		

		MyJDBC myJDBC = null;
		try {
			myJDBC = new MyJDBC();
			
			s_sql = "select max(paraid) from parameter";
			ps = myJDBC.prepareStatement(s_sql);
			rs = ps.executeQuery();
			rs.next();
			int s_count = rs.getInt(1);
			s_sql = null;
			rs.close();
			rs = null;
			ps.close();
			ps = null;
			s_sql = "select * from parameter where PARENTID=? and  UPPER(paraname)=? ";
			ps = myJDBC.prepareStatement(s_sql);
			ps.setString(1, pareID);
			ps.setString(2, key);
			rs = ps.executeQuery();
			if(rs.next()){
				toJson("1",response);
			}else{
			
	
			s_sql = "insert into parameter (paraid,parentid,paraname,status) values(?,?,?,?)";
			ps = myJDBC.prepareStatement(s_sql);

			ps.setString(1, Integer.toString(s_count + 1));
			ps.setString(2, pareID);
			ps.setString(3, key);
			ps.setString(4, "0");
			i_flag = ps.executeUpdate();
			List pr=new ArrayList();
			s_sql = "select paraid,paraname from PARAMETER where PARENTID=? and status=0 order by LISTNUM";
			ps = myJDBC.prepareStatement(s_sql);
			ps.setString(1, pareID);
			rs1 = ps.executeQuery();
			while(rs1.next()){
				Parameter  pa= new Parameter();
				pa.setParaid(rs1.getString(1));
				pa.setParname(rs1.getString(2));
				pr.add(pa);
			}
			InitParam.initParameter();
			toJson(pr,response);
			
			}
			} catch (Exception e) {
			e.printStackTrace();
			System.err.println("sql=" + s_sql);
		} finally {
			MyJDBC.close(myJDBC);
		}
	}
}
